package cn.dlc.com.account.service.impl;

import cn.dlc.com.account.dao.AccountDao;
import cn.dlc.com.account.entity.Account;
import cn.dlc.com.account.service.AccountService;
import cn.dlc.com.feign.RemoteAccountService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 邓立川
 * @date 2021/1/12 16:53
 * @desc 添加当前类的描述信息
 */
@Service
public class AccountServiceIml implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private RemoteAccountService remoteAccountService;

    @Override
    public void save(Account account) {
        accountDao.save(account);
    }

    @Transactional
    @GlobalTransactional
    @Override
    public void saveAll(Account account) {

        System.out.println(RootContext.getXID());

        // 保存本地数据
        this.save(account);

        // 保存远程数据
        String result = remoteAccountService.saveAccount(account);
        System.out.println("result" + result);
        if("fallback".equals(result)) {
            throw new RuntimeException("远程服务调用失败");
        }
        if("bank1error".equals(account.getAccountName())){
            throw new RuntimeException("bank1本地失败了");
        }
    }
}
